// CreepOp
// Copyright (c) 1998-2003 Matthew Faupel
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library Public License for more details.
//
// You should have received a copy of the GNU Library Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// 
// This Java class is inspired in part by the GAGS C++ library which I
// presume to be copyright J. J. Merelo <jmerelo@kal-el.ugr.es>.

package com.micropraxis.gajit;

/**
 * A class that represents a unary genetic operator that randomly adds or
 * subtracts one from the value of a random gene in a chromosome.
 * 
 * @author gajit@micropraxis.com
 * @version 0.2
 */
public class CreepOp extends GenOp
{
	public CreepOp()
	{
		super(false);
	}

	public Chrom apply(Chrom chrom1)
	{
		return applyAt(selectGene(chrom1), chrom1);
	}

	public Chrom applyAt(int index, Chrom chrom1)
	{
		Integer value;

		for (int i = 0; i < chrom1.get(index).size(); i++)
		{
			value = chrom1.get(index).get(i);

			if (Math.random() < 0.5)
				value = (value - 1) % 12;
			else
				value = (value + 1) % 12;

			chrom1.get(index).set(i, value);
		}

		return chrom1;
	}
}
